package EBM.UI;

import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import EBM.BLL.SocketConnection;
import EBM.BLL.Util;

public class EMQuery extends org.eclipse.swt.widgets.Composite {
	private Combo combVersion;
	private Combo cboSeverity;
	private Combo combStatu;
	private Label label7;
	private Label label6;
	private Combo combModel;
	private Combo combRespon;
	private Label label5;
	private Label label4;
	private Text txtSubmitPerson;
	private Label label3;
	private Label lblSubmitPerson;
	private Table table1;
	private TableColumn tableColumn1;
	private TableColumn tableColumn2;
	private TableColumn tableColumn3;
	private TableColumn tableColumn4;
	private TableColumn tableColumn5;
	private TableColumn tableColumn6;
	private Button btnCancel;
	private Button btnEnter;
	private Label label8;
	private Combo combCode;
	private Label lblCode;
	private Label lblDate;
	private Text txtID;
	private Label lblID;

	private ArrayList<String> queryBugAllProjectId = new ArrayList<String>();
	private ArrayList<String> queryBugAllProjectName = new ArrayList<String>();

	private ArrayList<String> queryBugAllUserId = new ArrayList<String>();
	private ArrayList<String> queryBugAllUserName = new ArrayList<String>();

	public static Display display;
	public static Shell shell;
	public static String userType;
	public static String userName;
	private MessageBox box = new MessageBox(shell, SWT.YES | SWT.ICON_WARNING);
	
	//增加
	private Group group;
	final private String NULLPARAM ="";
	private DateTime beginDate;
	private DateTime endDate;
	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	/*public static void main(String[] args) {
		showGUI();
	}*/

	/**
	 * Auto-generated method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void showGUI() {
		display = Display.getDefault();
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		EMQuery inst = new EMQuery(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		shell.setText("缺陷报告单查询");
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.setLocation(display.getClientArea().width / 2 - shell.getSize().x/2, 
				display.getClientArea().height / 2 - shell.getSize().y/2);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public EMQuery(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
		InitShow();
	}

	private void initGUI() {
		try {
			this.setLayout(null);
			this.setSize(732, 475);
			group = new Group(this,SWT.NONE);
			group.setBounds(17, 1, 688, 177);
			group.setText("查询条件");
			{
				btnCancel = new Button(group, SWT.PUSH | SWT.CENTER);
				btnCancel.setText("关闭");
				btnCancel.setBounds(386, 147, 104, 23);
				btnCancel.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						try{
							EMQuery.shell.close();
						}catch(Exception ex){
							ex.printStackTrace();
						}
					}
				});
			}
			{
				btnEnter = new Button(group, SWT.PUSH | SWT.CENTER);
				btnEnter.setText("查询");
				btnEnter.setBounds(193, 147, 104, 23);
				btnEnter.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						btnEnterWidgetSelected(evt);
					}
				});
			}
			{
				cboSeverity = new Combo(group, SWT.READ_ONLY);
				String[] items = {"致命","严重","一般","提示","任意"};
				cboSeverity.setItems(items);
				cboSeverity.setBounds(578, 76, 97, 25);
				cboSeverity.select(4);
			}
			{
				combStatu = new Combo(group, SWT.READ_ONLY);
				String[] items = 
				{"提交缺陷信息","项目经理审核","缺陷定位","缺陷修改","缺陷验证","缺陷关闭","缺陷挂起","任意"};
				combStatu.setItems(items);
				combStatu.setBounds(577, 32, 98, 25);
				combStatu.select(7);
			}
			{
				combModel = new Combo(group, SWT.READ_ONLY);
				combModel.setBounds(336, 74, 175, 25);
			}
			{
				combRespon = new Combo(group, SWT.READ_ONLY);
				combRespon.setBounds(400, 32, 79, 25);
			}
			{
				combVersion = new Combo(group, SWT.READ_ONLY);
				combVersion.setBounds(227, 74, 55, 25);
			}
			{
				txtSubmitPerson = new Text(group, SWT.BORDER);
				txtSubmitPerson.setBounds(231, 32, 83, 23);
			}
			{
				lblSubmitPerson = new Label(group, SWT.NONE);
				lblSubmitPerson.setText("提交人");
				lblSubmitPerson.setBounds(187, 32, 38, 18);
			}
			{
				table1 = new Table(this, SWT.NONE);
				table1.setBounds(17, 190, 688, 246);

				tableColumn1 = new TableColumn(table1, SWT.NONE);
				tableColumn2 = new TableColumn(table1, SWT.CENTER);
				tableColumn3 = new TableColumn(table1, SWT.CENTER);
				tableColumn4 = new TableColumn(table1, SWT.CENTER);
				tableColumn5 = new TableColumn(table1, SWT.CENTER);
				tableColumn6 = new TableColumn(table1, SWT.CENTER);
				tableColumn1.setText("");
				tableColumn2.setText("问题单号");
				tableColumn3.setText("当前责任人");
				tableColumn4.setText("当前状态");
				tableColumn5.setText("问题概要描述");
				tableColumn6.setText("停留天数");
				tableColumn1.setWidth(18);
				tableColumn2.setWidth(100);
				tableColumn3.setWidth(100);
				tableColumn4.setWidth(100);
				tableColumn5.setWidth(270);
				tableColumn6.setWidth(70);
				table1.setHeaderVisible(true);
				table1.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						table1WidgetSelected(evt);
					}
				});
			}
			{
				combCode = new Combo(group, SWT.READ_ONLY);
				combCode.setBounds(81, 73, 92, 25);
				combCode.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						combCodeWidgetSelected(evt);
					}
				});
			}
			{
				lblCode = new Label(group, SWT.NONE);
				lblCode.setText("项目ID");
				lblCode.setBounds(24, 76, 51, 15);
			}
			{
				lblDate = new Label(group, SWT.NONE);
				lblDate.setText("提交日期");
				lblDate.setBounds(24, 118, 54, 21);
			}
			{
				txtID = new Text(group, SWT.BORDER);
				txtID.setBounds(82, 32, 89, 23);
			}
			{
				lblID = new Label(group, SWT.NONE);
				lblID.setText("缺陷ID");
				lblID.setBounds(26, 32, 58, 18);
			}
			
			{
				label3 = new Label(group, SWT.NONE);
				label3.setText("版本");
				label3.setBounds(191, 76, 30, 16);
			}
			{
				label4 = new Label(group, SWT.NONE);
				label4.setText("当前责任人");
				label4.setBounds(332, 32, 63, 17);
			}
			{
				label5 = new Label(group, SWT.NONE);
				label5.setText("子系统");
				label5.setBounds(288, 76, 39, 15);
			}
			{
				label6 = new Label(group, SWT.NONE);
				label6.setText("缺陷单当前状态");
				label6.setBounds(489, 33, 86, 17);
			}
			{
				label7 = new Label(group, SWT.NONE);
				label7.setText("严重程度");
				label7.setBounds(523, 76, 49, 18);
			}
			{
				label8 = new Label(group, SWT.NONE);
				label8.setText("至");
				label8.setBounds(310, 120, 14, 24);
			}

			beginDate = new DateTime(group, SWT.DATE);
			beginDate.setBounds(107, 117, 93, 24);

			endDate = new DateTime(group, SWT.NONE);
			endDate.setBounds(420, 115, 93, 24);
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void InitShow(){
		try{
			queryBugAllProjectId.clear();
			queryBugAllProjectName.clear();
			SocketConnection.setCmd("getallproject");
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> in = SocketConnection.getListIn();

			if(in.get(0).toString().equals("-1")){
				String items[] = {"无项目"};
				combCode.setItems(items);
				return;
			} else{
				int count = in.size()/2;
				String items[] = new String[count];
				for(int i = 0;i < count; i++){
					queryBugAllProjectId.add(in.get(2*i).toString());
					queryBugAllProjectName.add(in.get(2*i+1).toString());
					items[i] = in.get(2*i).toString();
				}
				combCode.setItems(items);
				combCode.select(0);
				InitProjectDetail(combCode.getItem(combCode.getSelectionIndex()));
			}
			queryBugAllUserId.clear();
			queryBugAllUserName.clear();
			SocketConnection.setCmd("getalluser");
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> userin = SocketConnection.getListIn();

			if(userin.get(0).toString().equals("-1")){
				String items[] ={"无人员可选"};
				combRespon.setItems(items);
			} else{
				int userNum = userin.size()/2;
				String items[] = new String[userNum + 1];
				for(int i = 0;i < userNum; i++){
					queryBugAllUserId.add(userin.get(i*2).toString());
					queryBugAllUserName.add(userin.get(i*2+1).toString());
					//items[i] = userin.get(i*2+1).toString();
					items[i] = userin.get(i*2+1).toString()+"("
					           +userin.get(i*2).toString()+")";
				}
				items[userNum] = "任意";
				combRespon.setItems(items);
				combRespon.select(userNum);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void InitProjectDetail(String proid){
		try {
			if(combCode.getItem(combCode.getSelectionIndex()).equals("无项目")){
				combVersion.removeAll();
				combModel.removeAll();
				return;
			}
			SocketConnection.setCmd("getprojectdetailinfo");
			ArrayList<Object> out = new ArrayList<Object>();
			out.add(proid);
			SocketConnection.setListOut(out);
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> in = SocketConnection.getListIn();
			//显示项目所有信息
			//combVersion.setText(in.get(1).toString()); //version
			String[] versions = {in.get(1).toString()};
			combVersion.setItems(versions);
			combVersion.select(0);
			int modelNum = Integer.parseInt(in.get(2).toString());
			String []items = null;
			if(modelNum != 0){
				items = new String[modelNum + 1];
				for(int i = 0; i < modelNum; i++)
					items[i] = in.get(i + 3).toString();
				items[modelNum] = "任意";
				combModel.setItems(items);
				combModel.select(modelNum);
			}else
				combModel.removeAll();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}


	private void btnEnterWidgetSelected(SelectionEvent evt) {//缺陷查询
		table1.removeAll();
		if(beginDate.getYear()>endDate.getYear())
		{
			box.setMessage("注意查询的起始时间!");
			box.open();
			return;
		}
		else if(beginDate.getYear()==endDate.getYear())
		{
			if(beginDate.getMonth()>endDate.getMonth())
			{
				box.setMessage("注意查询的起始时间!");
				box.open();
				return;
			}
			else if(beginDate.getMonth()==endDate.getMonth())
			{
				if(beginDate.getDay()>endDate.getDay())
				{
					box.setMessage("注意查询的起始时间!");
					box.open();
					return;
				}
			}
		}
		
		String date1 = beginDate.getYear() + "-" + (beginDate.getMonth()+1) + "-" + beginDate.getDay();
		String date2 = endDate.getYear() + "-" + (endDate.getMonth()+1) + "-" + endDate.getDay();
		
		
		try {
			SocketConnection.setCmd("querybugreport");
			ArrayList<Object> out = new ArrayList<Object>();
			out.add(combCode.getText());//project id
			if(txtID.getText().trim().equals(""))  //bug id
				out.add(NULLPARAM);
			else 
				out.add(txtID.getText().substring(1));
			if(txtSubmitPerson.getText().trim().equals(""))//提交人
				out.add(NULLPARAM);
			else 
				out.add(txtSubmitPerson.getText());
			if(combRespon.getText().equals("任意"))//责任人
				out.add(NULLPARAM);
			else
				/*out.add(queryBugAllUserId.get(
						Util.findIdByStr(combRespon.getItem(
								combRespon.getSelectionIndex()),queryBugAllUserName)));*/
				out.add(Util.splitUserNameId(combRespon.getItem(combRespon.getSelectionIndex()))[1]);
			if(combStatu.getText().equals("任意"))//bug状态
				out.add(NULLPARAM);
			else
				out.add(combStatu.getSelectionIndex());
			if(combModel.getText().trim().equals("")
					||combModel.getText().equals("任意"))
				out.add(NULLPARAM);
			else
				out.add(combModel.getText());
			if(cboSeverity.getText().trim().equals("任意"))
				out.add(NULLPARAM);
			else
				out.add(cboSeverity.getSelectionIndex());
			out.add(date1);
			out.add(date2);
			SocketConnection.setListOut(out);
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> in = SocketConnection.getListIn();
			int bugNum = in.size()/6;

			//Color color = display.getSystemColor(SWT.COLOR_GRAY);
			Color color = new Color(display,new RGB(222,226,226));
			for(int i = 0; i < bugNum; i++){
				TableItem item = new TableItem(table1, SWT.NONE);
				item.setText(new String[] {
						String.valueOf(i), 
						in.get(i*6).toString(), 
						in.get(i*6+2).toString(),
						Util.convertToStatusName(in.get(i*6+3).toString()),
						in.get(i*6+4).toString(),
						in.get(i*6+5).toString()});
				if(i%2 == 0)
					item.setBackground(color);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void combCodeWidgetSelected(SelectionEvent evt) {
		InitProjectDetail(combCode.getText());
	}
	
	private void table1WidgetSelected(SelectionEvent evt) {
		try{
			EMProcess.userType = EMQuery.userType;
			EMProcess.userName = EMQuery.userName;
			EMProcess.ebmsID = table1.getSelection()[0].getText(1);
			EMProcess.days = table1.getSelection()[0].getText(5);
			EMProcess.responpeo = table1.getSelection()[0].getText(2);
			EMProcess.status = Util.convertToStatusId(table1.getSelection()[0].getText(3));
			EMProcess.wtgyms = table1.getSelection()[0].getText(4);
			EMProcess.proID = combCode.getItem(combCode.getSelectionIndex());
			EMProcess.isEdit = "false";
			EMProcess.showGUI();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
